package com.lottery.ruok.lottrey.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.PrizePeopleAdapter;
import com.lottery.ruok.lottrey.eventbusObject.GivePrizeSuccess;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetRoomUsersRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;
import io.rong.imlib.model.Conversation;
import ui.NavigateBar;
/*
* 打赏记录
* */
public class SearchGivePrizeActivity extends BaseActivity {
    public static final String EXTRA_CONVERSATION_TYPE = "extra_conversation_type";

    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    @BindView(R.id.search_text)
    EditText searchText;
    @BindView(R.id.list)
    ListView list;

    private Disposable disposable;
    private PrizePeopleAdapter prizePeopleAdapter;
    private GetRoomUsersResponse getRoomUsersResponsess;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(final GivePrizeSuccess event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_give_prize);
        ButterKnife.bind(this);

        EventBus.getDefault().register(this);

        navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
            @Override
            public void onOperateClick(View view) {
                Intent intent = new Intent(SearchGivePrizeActivity.this, MyRedPacketActivity.class);
                intent.putExtra("isThreeTab", true);
                startActivity(intent);
            }
        });
        searchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length() == 0){
                    getList(null);
                } else {
                    getList(editable.toString());
                }
            }
        });

        getList(null);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              /*  Intent intent = new Intent(SearchGivePrizeActivity.this, GiveRewardActivity.class);
                intent.putExtra("avatar", getRoomUsersResponsess.getList().get(i).getAvatar());
                intent.putExtra("user", getRoomUsersResponsess.getList().get(i).getUsername());*/
                GetRoomUsersResponse.ListBean toUserInfo = getRoomUsersResponsess.getList().get(i);
                io.rong.imlib.model.UserInfo rongCloudUserinfo = new io.rong.imlib.model.UserInfo(
                        toUserInfo.getRy_id(),
                        toUserInfo.getUsername(),
                        toUserInfo.getAvatar()!=null?Uri.parse(toUserInfo.getAvatar()):null
                );

                Intent intent = new Intent(SearchGivePrizeActivity.this, GiveRewardActivity.class);
                intent.putExtra(GiveRewardActivity.EXTRA_CONVERSATION_TYPE, "chatroom");
                intent.putExtra(GiveRewardActivity.EXTRA_USER, rongCloudUserinfo);
                intent.putExtra(GiveRewardActivity.EXTRA_TARGET_ID, UserInfo.getRoomId());
                startActivity(intent);
                finish();
            }
        });
    }

    private void getList(String s){
        if(disposable != null){
            disposable.dispose();
        }
        disposable = RetrofitBase.getRetrofit().create(GetRoomUsersRequest.class)
                .getResult(UserInfo.getToken(), UserInfo.getRoomId(), s, 1000000, 0)
                .compose(RxHelper.<GetRoomUsersResponse>handleResult())
                .subscribeWith(new RxSubscribe<GetRoomUsersResponse>(this) {
                    @Override
                    protected void _onNext(GetRoomUsersResponse getRoomUsersResponses) {
                        getRoomUsersResponsess = getRoomUsersResponses;
                        if(prizePeopleAdapter == null){
                            prizePeopleAdapter = new PrizePeopleAdapter(SearchGivePrizeActivity.this, getRoomUsersResponses.getList());
                            list.setAdapter(prizePeopleAdapter);
                        } else {
                            prizePeopleAdapter.setList(getRoomUsersResponses.getList());
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }

    public static void startActivity(Context context, String targetId, Conversation.ConversationType conversationType) {
        Intent intent = new Intent(context, SearchGivePrizeActivity.class);
        context.startActivity(intent);
    }
}